<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>DELETE</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="DECLARE"
HREF="sql-declare.html"><LINK
REL="NEXT"
TITLE="DISCARD"
HREF="sql-discard.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="DECLARE"
HREF="sql-declare.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="DISCARD"
HREF="sql-discard.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-DELETE"
></A
>DELETE</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN69733"
></A
><H2
>Name</H2
>DELETE&nbsp;--&nbsp;delete rows of a table</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN69738"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>[ WITH [ RECURSIVE ] <TT
CLASS="REPLACEABLE"
><I
>with_query</I
></TT
> [, ...] ]
DELETE FROM [ ONLY ] <TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
> [ [ AS ] <TT
CLASS="REPLACEABLE"
><I
>alias</I
></TT
> ]
    [ USING <TT
CLASS="REPLACEABLE"
><I
>using_list</I
></TT
> ]
    [ WHERE <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
> | WHERE CURRENT OF <TT
CLASS="REPLACEABLE"
><I
>cursor_name</I
></TT
> ]
    [ RETURNING * | <TT
CLASS="REPLACEABLE"
><I
>output_expression</I
></TT
> [ [ AS ] <TT
CLASS="REPLACEABLE"
><I
>output_name</I
></TT
> ] [, ...] ]</PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN69748"
></A
><H2
>Description</H2
><P
>   <TT
CLASS="COMMAND"
>DELETE</TT
> deletes rows that satisfy the
   <TT
CLASS="LITERAL"
>WHERE</TT
> clause from the specified table.  If the
   <TT
CLASS="LITERAL"
>WHERE</TT
> clause is absent, the effect is to delete
   all rows in the table.  The result is a valid, but empty table.
  </P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
>     <A
HREF="sql-truncate.html"
>TRUNCATE</A
> is a
     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> extension that provides a
     faster mechanism to remove all rows from a table.
    </P
></BLOCKQUOTE
></DIV
><P
>   By default, <TT
CLASS="COMMAND"
>DELETE</TT
> will delete rows in the
   specified table and all its child tables. If you wish to delete only
   from the specific table mentioned, you must use the
   <TT
CLASS="LITERAL"
>ONLY</TT
> clause.
  </P
><P
>   There are two ways to delete rows in a table using information
   contained in other tables in the database: using sub-selects, or
   specifying additional tables in the <TT
CLASS="LITERAL"
>USING</TT
> clause.
   Which technique is more appropriate depends on the specific
   circumstances.
  </P
><P
>   The optional <TT
CLASS="LITERAL"
>RETURNING</TT
> clause causes <TT
CLASS="COMMAND"
>DELETE</TT
>
   to compute and return value(s) based on each row actually deleted.
   Any expression using the table's columns, and/or columns of other
   tables mentioned in <TT
CLASS="LITERAL"
>USING</TT
>, can be computed.
   The syntax of the <TT
CLASS="LITERAL"
>RETURNING</TT
> list is identical to that of the
   output list of <TT
CLASS="COMMAND"
>SELECT</TT
>.
  </P
><P
>   You must have the <TT
CLASS="LITERAL"
>DELETE</TT
> privilege on the table
   to delete from it, as well as the <TT
CLASS="LITERAL"
>SELECT</TT
>
   privilege for any table in the <TT
CLASS="LITERAL"
>USING</TT
> clause or
   whose values are read in the <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
>.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN69774"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>with_query</I
></TT
></DT
><DD
><P
>      The <TT
CLASS="LITERAL"
>WITH</TT
> clause allows you to specify one or more
      subqueries that can be referenced by name in the <TT
CLASS="COMMAND"
>DELETE</TT
>
      query. See <A
HREF="queries-with.html"
>Section 7.8</A
> and <A
HREF="sql-select.html"
>SELECT</A
>
      for details.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>ONLY</TT
></DT
><DD
><P
>      If specified, delete rows from the named table only.  When not
      specified, any tables inheriting from the named table are also processed.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
></DT
><DD
><P
>      The name (optionally schema-qualified) of an existing table.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>alias</I
></TT
></DT
><DD
><P
>      A substitute name for the target table. When an alias is
      provided, it completely hides the actual name of the table.  For
      example, given <TT
CLASS="LITERAL"
>DELETE FROM foo AS f</TT
>, the remainder
      of the <TT
CLASS="COMMAND"
>DELETE</TT
> statement must refer to this
      table as <TT
CLASS="LITERAL"
>f</TT
> not <TT
CLASS="LITERAL"
>foo</TT
>.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>using_list</I
></TT
></DT
><DD
><P
>      A list of table expressions, allowing columns from other tables
      to appear in the <TT
CLASS="LITERAL"
>WHERE</TT
> condition.  This is similar
      to the list of tables that can be specified in the <A
HREF="sql-select.html#SQL-FROM"
><I
><I
>FROM</I
> Clause</I
></A
> of a
      <TT
CLASS="COMMAND"
>SELECT</TT
> statement; for example, an alias for
      the table name can be specified.  Do not repeat the target table
      in the <TT
CLASS="REPLACEABLE"
><I
>using_list</I
></TT
>,
      unless you wish to set up a self-join.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
></DT
><DD
><P
>      An expression that returns a value of type <TT
CLASS="TYPE"
>boolean</TT
>.
      Only rows for which this expression returns <TT
CLASS="LITERAL"
>true</TT
>
      will be deleted.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>cursor_name</I
></TT
></DT
><DD
><P
>      The name of the cursor to use in a <TT
CLASS="LITERAL"
>WHERE CURRENT OF</TT
>
      condition.  The row to be deleted is the one most recently fetched
      from this cursor.  The cursor must be a non-grouping
      query on the <TT
CLASS="COMMAND"
>DELETE</TT
>'s target table.
      Note that <TT
CLASS="LITERAL"
>WHERE CURRENT OF</TT
> cannot be
      specified together with a Boolean condition.  See
      <A
HREF="sql-declare.html"
>DECLARE</A
>
      for more information about using cursors with
      <TT
CLASS="LITERAL"
>WHERE CURRENT OF</TT
>.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>output_expression</I
></TT
></DT
><DD
><P
>      An expression to be computed and returned by the <TT
CLASS="COMMAND"
>DELETE</TT
>
      command after each row is deleted.  The expression can use any
      column names of the <TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
>
      or table(s) listed in <TT
CLASS="LITERAL"
>USING</TT
>.
      Write <TT
CLASS="LITERAL"
>*</TT
> to return all columns.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>output_name</I
></TT
></DT
><DD
><P
>      A name to use for a returned column.
     </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN69845"
></A
><H2
>Outputs</H2
><P
>   On successful completion, a <TT
CLASS="COMMAND"
>DELETE</TT
> command returns a command
   tag of the form
</P><PRE
CLASS="SCREEN"
>DELETE <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
></PRE
><P>
   The <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
> is the number
   of rows deleted.  If <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
> is
   0, no rows matched the <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
> (this is not considered
   an error).
  </P
><P
>   If the <TT
CLASS="COMMAND"
>DELETE</TT
> command contains a <TT
CLASS="LITERAL"
>RETURNING</TT
>
   clause, the result will be similar to that of a <TT
CLASS="COMMAND"
>SELECT</TT
>
   statement containing the columns and values defined in the
   <TT
CLASS="LITERAL"
>RETURNING</TT
> list, computed over the row(s) deleted by the
   command.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN69859"
></A
><H2
>Notes</H2
><P
>   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> lets you reference columns of
   other tables in the <TT
CLASS="LITERAL"
>WHERE</TT
> condition by specifying the
   other tables in the <TT
CLASS="LITERAL"
>USING</TT
> clause.  For example,
   to delete all films produced by a given producer, one can do:
</P><PRE
CLASS="PROGRAMLISTING"
>DELETE FROM films USING producers
  WHERE producer_id = producers.id AND producers.name = 'foo';</PRE
><P>
   What is essentially happening here is a join between <TT
CLASS="STRUCTNAME"
>films</TT
>
   and <TT
CLASS="STRUCTNAME"
>producers</TT
>, with all successfully joined
   <TT
CLASS="STRUCTNAME"
>films</TT
> rows being marked for deletion.
   This syntax is not standard.  A more standard way to do it is:
</P><PRE
CLASS="PROGRAMLISTING"
>DELETE FROM films
  WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');</PRE
><P>
   In some cases the join style is easier to write or faster to
   execute than the sub-select style.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN69870"
></A
><H2
>Examples</H2
><P
>   Delete all films but musicals:
</P><PRE
CLASS="PROGRAMLISTING"
>DELETE FROM films WHERE kind &lt;&gt; 'Musical';</PRE
><P>
  </P
><P
>   Clear the table <TT
CLASS="LITERAL"
>films</TT
>:
</P><PRE
CLASS="PROGRAMLISTING"
>DELETE FROM films;</PRE
><P>
  </P
><P
>   Delete completed tasks, returning full details of the deleted rows:
</P><PRE
CLASS="PROGRAMLISTING"
>DELETE FROM tasks WHERE status = 'DONE' RETURNING *;</PRE
><P>
  </P
><P
>   Delete the row of <TT
CLASS="STRUCTNAME"
>tasks</TT
> on which the cursor
   <TT
CLASS="LITERAL"
>c_tasks</TT
> is currently positioned:
</P><PRE
CLASS="PROGRAMLISTING"
>DELETE FROM tasks WHERE CURRENT OF c_tasks;</PRE
><P></P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN69883"
></A
><H2
>Compatibility</H2
><P
>   This command conforms to the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard, except
   that the <TT
CLASS="LITERAL"
>USING</TT
> and <TT
CLASS="LITERAL"
>RETURNING</TT
> clauses
   are <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> extensions, as is the ability
   to use <TT
CLASS="LITERAL"
>WITH</TT
> with <TT
CLASS="COMMAND"
>DELETE</TT
>.
  </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-declare.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-discard.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>DECLARE</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>DISCARD</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>